<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn">点我获取最新时间</button>
    <div id="time">当前时间是：</div>
    <script>
        let xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        document.getElementById("btn").onclick = function () {
            xmlhttp.open("GET", "http://localhost:8000/api/time?d="+Date.now(),true)
            //设置请求头
            xmlhttp.setRequestHeader('Accept', 'text/html')
            //请求的回调：当服务器响应了数据之后会执行该回调函数
            xmlhttp.onreadystatechange = function () {
                if (this.readyState === 4) {
                    console.log(this.getAllResponseHeaders()) 
                    // console.log(this.responseText, this.status)
                    var { data } = JSON.parse(this.responseText);
                    document.getElementById("time").innerHTML = data.time;
                }
            }
            //发送请求
            xmlhttp.send(null)  
        }
        
    </script>
</body>
</html>